package com.lzx.shop.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzx.shop.dataobject.PaymentRecordDO;
import com.lzx.shop.dataobject.UserDO;
import com.lzx.shop.enums.OrderStatus;
import com.lzx.shop.model.Result;
import com.lzx.shop.service.OrderService;
import com.lzx.shop.service.PaymentRecordService;
import com.lzx.shop.service.UserService;
import com.lzx.shop.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName PaymentRecordApi
 * @Author 刘正星
 * @Date 2021/3/27 下午4:17
 * @Description
 */
@Api(tags = "paymentRecordApi",description = "支付记录")
@RestController
@RequestMapping("/paymentRecord")
public class PaymentRecordApi {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PaymentRecordService paymentRecordService;

    @Autowired
    private UserService userService;

    @ApiOperation("查询所有支付记录")
    @GetMapping("/list")
    public Result list() {

        List<PaymentRecordDO> all = paymentRecordService.findAll();

        return ResultUtil.success(all);
    }

    @ApiOperation("插入支付记录")
    @PostMapping("/save")
    public Result save(@RequestBody PaymentRecordDO object, HttpSession session) {
        if (object == null) {
            return ResultUtil.paramError();
        }

        String orderNumber = object.getOrderNumber();

        orderService.updateProductPersonNumber(orderNumber);

        orderService.updateOrderStatus(orderNumber, OrderStatus.TRADE_PAID_SUCCESS);

//        UserDO userInfo = (UserDO) session.getAttribute("userInfo");

//        object.setUserId(userInfo.getId());

        return ResultUtil.success(paymentRecordService.save(object));
    }

    @ApiOperation("更新支付记录")
    @PostMapping("/update")
    public Result update(@RequestBody PaymentRecordDO object) {
        if (object == null) {
            return ResultUtil.paramError();
        }
        return ResultUtil.success(paymentRecordService.updateStatus(object));
    }


    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result page(@RequestParam("page")Integer page,@RequestParam("limit")Integer limit) {

       int pageNum = page== null ? 1 : page;
       int pageSize = limit == null ? 10 : limit;

        Page<PaymentRecordDO> pageI = new Page<>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        IPage<PaymentRecordDO> paymentRecordDOIPage = paymentRecordService.page(pageI, null);
        List<String> userIds = paymentRecordDOIPage.getRecords().stream().map(PaymentRecordDO::getUserId).distinct().collect(Collectors.toList());

        Map<String, UserDO> objectMap = userService.getByIds(userIds).stream().collect(Collectors.toMap(e -> e.getId(),e->e));
        paymentRecordDOIPage.getRecords().forEach(paymentRecordDO -> {
            paymentRecordDO.setUserDO(objectMap.get(paymentRecordDO.getUserId()));
        });
        return ResultUtil.success(paymentRecordDOIPage);
    }


}
